//
//  ModifyPasswordController.m
//  YSSmartHome
//
//  Created by yanghao on 2017/4/11.
//  Copyright © 2017年 yahaong. All rights reserved.
//

#import "ModifyPasswordController.h"
#import "YSCommon.h"
#import "SureView.h"
#import "UIView+HUD.h"
#import "MQTTSessionManager.h"
#import <XAISDK/XAI.h>

@interface ModifyPasswordController ()
@property (nullable, nonatomic, strong) UITextField* mOldPwdTextFile;
@property (nullable, nonatomic, strong) UITextField* mNewPwdTextFile;
@property (nullable, nonatomic, strong) UITextField* mSureNewPwdTextFile;
@property (nullable, nonatomic, strong) SureView* mSureView;
@property (nullable, nonatomic, strong) MQTTSessionManager* sessionManager;
@end

@implementation ModifyPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (!kMQTTSessionManager.xai) {
        kMQTTSessionManager.xai = [[XAI alloc]initWithHost:kZTMQTTBaseURL port:kZTMQTTBasePort];
    }
    [self initSubViews];
    __weak typeof(self)wself = self;
    self.mSureView.sureBtnClickBlock = ^{
        __strong typeof(wself)sself = wself;
        if (!sself) {
            return;
        }
        if (sself.mOldPwdTextFile.text.length < 6)
        {
            [sself.view yh_showMsgHUD: @"原密码长度不对"];
            return;
        }
        if (sself.mNewPwdTextFile.text.length < 6)
        {
            [sself.view yh_showMsgHUD: @"新密码格式不对"];
            return;
        }
        if (![sself.mNewPwdTextFile.text isEqualToString:sself.mSureNewPwdTextFile.text])
        {
            [sself.view yh_showMsgHUD: @"新密码不一致"];
            return;
        }
        [sself.view yh_showLoadingHUD];
        [kMQTTSessionManager.xai passwordChangeWithAccount:sself.sessionManager.currentUser.m_strTel newPwd:sself.mSureNewPwdTextFile.text oldPwd:sself.mOldPwdTextFile.text success:^{
            [sself.view yh_hideAllHUD];
            [sself.view yh_showMsgHUD: @"修改成功"];
            [sself.sessionManager.xai close];
            [[NSNotificationCenter defaultCenter]postNotificationName:kZTExitLoginNotification object:nil];
        } error:^(NSError * _Nonnull error) {
            [sself.view yh_hideAllHUD];
            [sself.view yh_showMsgHUD: error.userInfo[@"errormsg"]];
        }];
    };
}

- (void)initSubViews
{
    self.navigationItem.title = @"修改密码";
    self.view.backgroundColor = kBgColor;
    [self.view addSubview:self.mOldPwdTextFile];
    [self.view addSubview:self.mNewPwdTextFile];
    [self.view addSubview:self.mSureNewPwdTextFile];
    [self.view addSubview:self.mSureView];
    [self layoutPageSubViews];
}
- (void)layoutPageSubViews
{
    [self.mOldPwdTextFile mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view).offset(UIEdgeInsetsMake(10, 0, 0, 0));
        make.height.equalTo(kIphoneSize_Height(44));
    }];
    [self.mNewPwdTextFile mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.leading.trailing.equalTo(self.mOldPwdTextFile);
        make.top.equalTo(self.mOldPwdTextFile.bottom).offset(kIphoneSize_Height(10));
    }];
    [self.mSureNewPwdTextFile mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.leading.trailing.equalTo(self.mOldPwdTextFile);
        make.top.equalTo(self.mNewPwdTextFile.bottom).offset(kIphoneSize_Height(10));
    }];
    [self.mSureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.mSureNewPwdTextFile.bottom).offset(kIphoneSize_Height(60));
        make.height.equalTo(100);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - getters and stters 

- (UITextField *)mOldPwdTextFile
{
    if (!_mOldPwdTextFile) {
        _mOldPwdTextFile = [[UITextField alloc]init];
        _mOldPwdTextFile.placeholder = @"请输入原密码";
        _mOldPwdTextFile.backgroundColor = [UIColor whiteColor];
        UILabel* label = [[UILabel alloc]init];
        label.text = @"原 密 码";
        label.frame = CGRectMake(0, 0, 100, 44);
        label.textAlignment = NSTextAlignmentCenter;
        _mOldPwdTextFile.leftView = label;
        _mOldPwdTextFile.leftViewMode = UITextFieldViewModeAlways;
        _mOldPwdTextFile.secureTextEntry = YES;
        _mOldPwdTextFile.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _mOldPwdTextFile;
}
- (UITextField *)mNewPwdTextFile
{
    if (!_mNewPwdTextFile) {
        _mNewPwdTextFile = [[UITextField alloc]init];
        _mNewPwdTextFile.placeholder = @"请输入新密码至少6位";
        _mNewPwdTextFile.backgroundColor = [UIColor whiteColor];
        UILabel* label = [[UILabel alloc]init];
        label.text = @"新 密  码";
        label.frame = CGRectMake(0, 0, 100, 44);
        label.textAlignment = NSTextAlignmentCenter;
        _mNewPwdTextFile.leftView = label;
        _mNewPwdTextFile.leftViewMode = UITextFieldViewModeAlways;
        _mNewPwdTextFile.secureTextEntry = YES;
        _mNewPwdTextFile.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _mNewPwdTextFile;
}

- (UITextField *)mSureNewPwdTextFile
{
    if (!_mSureNewPwdTextFile) {
        _mSureNewPwdTextFile = [[UITextField alloc]init];
        _mSureNewPwdTextFile.placeholder = @"请再次输入新密码";
        _mSureNewPwdTextFile.backgroundColor = [UIColor whiteColor];
        UILabel* label = [[UILabel alloc]init];
        label.text = @"确认密码";
        label.frame = CGRectMake(0, 0, 100, 44);
        label.textAlignment = NSTextAlignmentCenter;
        _mSureNewPwdTextFile.leftView = label;
        _mSureNewPwdTextFile.leftViewMode = UITextFieldViewModeAlways;
        _mSureNewPwdTextFile.secureTextEntry = YES;
        _mSureNewPwdTextFile.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _mSureNewPwdTextFile;
}

- (SureView *)mSureView
{
    if (!_mSureView) {
        _mSureView = [[SureView alloc]init];
        _mSureView.title = @"确认";
    }
    return _mSureView;
}
- (MQTTSessionManager *)sessionManager
{
    if (!_sessionManager) {
        _sessionManager = [MQTTSessionManager sharedManager];
    }
    return _sessionManager;
}
@end
